#       OPMLParser_test.py
#       
#       Copyright 2008 Joshua Scotton <josh@joshuascotton.com>
#       
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#       
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#       
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

__licence__ = "GPLv2"
__copyright__ = "2008, Joshua Scotton"
__author__ = "Joshua Scotton <josh@joshuascotton.com>"

import unittest
import sys
import os
import logging
import xml.parsers.expat

sys.path += [os.path.abspath('..')]

from utils.feed_utils import OPMLParser

class OPMLParserTest(unittest.TestCase):

    def setUp(self, debug=False):
        """Sets up the paths, lists and logger that will be used in the tests"""
        self.debug = debug
        if(self.debug):
            #set up logger
            self.logger = logging.getLogger('Entertainer')
            fileHandle = logging.FileHandler('./tests.log')
            fileHandle.setFormatter(logging.Formatter('%(asctime)s %(levelname)s %(funcName)s %(message)s'))
            self.logger.addHandler(fileHandle)
            self.logger.setLevel(logging.DEBUG)

        #set up lists and paths
        self.testURL = "http://www.scripting.com/feeds/top100.opml"
        self.multipleLifereaFolders = os.path.abspath('.') + '/data/OPMLParser/lifereaMultiple'
        self.singleLifereaFolder = os.path.abspath('.') + '/data/OPMLParser/lifereaSingle'
        self.noLifereaFolder = os.path.abspath('.') + '/data/OPMLParser/lifereaNoFolder'
        self.noLifereaFile = os.path.abspath('.') + '/data/OPMLParser/lifereaNoFile'
        self.okFilePath = os.path.abspath('.') + '/data/OPMLParser/test.opml'
        self.badFilePath = os.path.abspath('.') + '/data/OPMLParser/null.opml'
        self.notXMLFile = os.path.abspath('.') + '/data/OPMLParser/noXML.txt'
        self.insideFilePath = os.path.abspath('.') + '/data/OPMLParser/opmlInOpml.opml'
        self.feedList = ["http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml","http://www.hackaday.com/rss.xml","http://edevelop.org/planet/rss20.xml","http://feeds.feedburner.com/BlenderartMagazine"]
    
    def testGetRssFeeds001(self):
        """testGetRssFeeds001: Check if OPMLParser can parse a normal file"""
        output = OPMLParser().get_rss_feeds(self.okFilePath)
        if(self.debug):
            self.logger.debug('Expecting the list : ' + str(self.feedList))
            self.logger.debug('OUTPUT:' + str(output))
        self.assertEqual(output, self.feedList)
    
    def testGetRssFeeds002(self):
        """testGetRssFeeds002: Check if OPMLParser can handle a non-existant file"""
        if(self.debug):
            self.logger.debug('Expecting an IOError')
        self.assertRaises(IOError, OPMLParser().get_rss_feeds, self.badFilePath)
        
    def testGetRssFeeds003(self):
        """testGetRssFeeds003: Check if OPMLParser discards opml links inside the file
        This is the same file as for testGetRssFeeds001 with an extra link to an opml file.
        The OPMLParser should discard the opml file and return the rest of the links"""
        output = OPMLParser().get_rss_feeds(self.insideFilePath)
        if(self.debug):
            self.logger.debug('Expecting the list : ' + str(self.feedList))
            self.logger.debug('OUTPUT: ' + str(output))
        self.assertEqual(output, self.feedList)
    
    def testGetRssFeeds004(self):
        """testGetRssFeeds004: Check if OPMLParser can handle a file which is not an opml file or even an xml file"""
        if(self.debug):
            self.logger.debug('Expecting an ExpatError')
        self.assertRaises(xml.parsers.expat.ExpatError, OPMLParser().get_rss_feeds, self.notXMLFile)
    
    def testGetRssFeeds005(self):
        """testGetRssFeeds005: Check if OPMLParser can handle a URL"""
        output = OPMLParser().get_rss_feeds(self.testURL)
        if(self.debug):
            self.logger.debug('Expecting anything but []')
            self.logger.debug('OUTPUT: ' + str(output))
        self.assertTrue(output <> [])
        
    def testGetLifereaOpml001(self):
        """testGetLifereaOpml001: Check if OPMLParser can pick the opml file for liferea"""
        output = OPMLParser().get_liferea_opml(self.singleLifereaFolder)
        if(self.debug):
            self.logger.debug('Expecting the string : ' + self.singleLifereaFolder+"/.liferea/feedlist.opml")
            self.logger.debug('OUTPUT: ' + output)
        self.assertEqual(output, self.singleLifereaFolder+"/.liferea/feedlist.opml")
        
    def testGetLifereaOpml002(self):
        """testGetLifereaOpml002: Check if OPMLParser can pick the opml file for the latest version of liferea out of a selection of folders"""
        output = OPMLParser().get_liferea_opml(self.multipleLifereaFolders)
        if(self.debug):
            self.logger.debug('Expecting the string : ' + self.multipleLifereaFolders+"/.liferea_1.4/feedlist.opml")
            self.logger.debug('OUTPUT: ' + output)
        self.assertEqual(output, self.multipleLifereaFolders+"/.liferea_1.4/feedlist.opml")
    
    def testGetLifereaOpml003(self):
        """testGetLifereaOpml003: Check if OPMLParser returns an empty string if it can't find the liferea opml folder"""
        output = OPMLParser().get_liferea_opml(self.noLifereaFolder)
        if(self.debug):
            self.logger.debug('Expecting an empty string')
            self.logger.debug('OUTPUT: ' + output)
        self.assertEqual(output, "")

    def testGetLifereaOpml004(self):
        """testGetLifereaOpml004: Check if OPMLParser returns an empty string if it can't find the liferea opml file"""
        output = OPMLParser().get_liferea_opml(self.noLifereaFile)
        if(self.debug):
            self.logger.debug('Expecting an empty string')
            self.logger.debug('OUTPUT: ' + output)
        self.assertEqual(output, "")        
